<?php
	session_start();
	require 'include/configDB.php';
	$id_pengguna = $_SESSION['id_pengguna'];
	//echo "$id_pengguna";
	$query_wali = mysql_query("SELECT * FROM penugasan_wali WHERE NIK='$id_pengguna'");
	$wali_data = mysql_fetch_assoc($query_wali);
	$id_kelas = $wali_data['id_kelas'];
	$query_kelas = mysql_query("SELECT * FROM kelas WHERE id='$id_kelas'");
	$kelas_data = mysql_fetch_assoc($query_kelas);
	$id_jurusan = $kelas_data['id_jurusan'];
	$jurusan_query = mysql_query("SELECT * FROM jurusan WHERE id='$id_jurusan'");
	$jurusan_data = mysql_fetch_assoc($jurusan_query);
	$semester = $_POST['semester'];
	// echo "$id_kelas";
	// exit;
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Laporan Nilai Wali Kelas</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="clockp.js"></script>
<script type="text/javascript" src="clockh.js"></script> 
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="ddaccordion.js"></script>
<script type="text/javascript">
ddaccordion.init({
	headerclass: "submenuheader", //Shared CSS class name of headers group
	contentclass: "submenu", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [], //index of content(s) open by default [index1, index2, etc] [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", ""], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["suffix", "<img src='images/plus.gif' class='statusicon' />", "<img src='images/minus.gif' class='statusicon' />"], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})
</script>

<script type="text/javascript" src="jconfirmaction.jquery.js"></script>
<script type="text/javascript">
	
	$(document).ready(function() {
		$('.ask').jConfirmAction();
	});
	
</script>

<script language="javascript" type="text/javascript" src="niceforms.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="niceforms-default.css" />
<style>
	<?php include 'include_style.php'; ?>
</style>
</head>
<body>
<div id="main_container">

	<?php include 'header.php'; ?>
    
    <div class="main_content">
                
                    
    <div class="center_content">  
		
    
	
    <div class="left_content">
    
    		<?php include 'left_content.php'; ?>
    		<?php include 'calendar.php' ; ?>

    </div>  
    
    <div class="right_content">
	<h2>Data Nilai Akademik</h2>
	<h4>Laporan nilai berdasarkan Nama Siswa</h4>
	<form name="wali_kelas_form" id="wali_kelas_form" method="post" action="laporan_data_nilaiWakes.php">
		<table>
			<tr>
				<td>
					Kelas 
				</td>
				<td>
					:
				</td>
				<td width="150">
					<?=$kelas_data['tingkat']?>
				</td>
				<td>
					Jurusan
				</td>
				<td>
					:
				</td>
				<td>
					<?=$jurusan_data['nama']?>
				</td>
				<td></td>
				<td>
					<?=$kelas_data['nomor']?>
				</td>
			</tr>
			<tr>	
				<td>
					Semester
				</td>
				<td>:</td>
				<td>
					<?=$semester?>
				</td>
			</tr>
			</tr> 
		</table>
		</form>

		<form method="post">
		</br>
		</br>
		<table id="rounded-corner" summary="2007 Major IT Companies' Profit">
			<thead>
				<tr>
					<th scope="col" class="rounded">No</th>
					<th scope="col" class="rounded">NIS</th>
					<th scope="col" class="rounded">Nama Siswa</th>
					<th scope="col" class="rounded">Rata-rata nilai akhir</th>
					<th scope="col" class="rounded">Ranking</th>
				</tr>
			</thead>
				<?php
					// 
					$query_siswa = mysql_query("SELECT * FROM siswa WHERE id_kelas='$id_kelas'");
					$rank = array();
					$i = 0;
					while($siswa_data = mysql_fetch_assoc($query_siswa))
					{
						$semester = $_POST['semester'];
						$id_siswa = $siswa_data['NIS'];
						$nilai_query = mysql_query("SELECT AVG(nilai) AS average FROM nilai WHERE tipe_nilai='Nilai Akhir' AND id_kelas='$id_kelas' AND id_siswa='$id_siswa' AND semester='$semester' ");
						$data_nilai_akhir = mysql_fetch_assoc($nilai_query);
						$rank[$i]['nilai'] = $data_nilai_akhir['average'];
						$rank[$i]['id_siswa'] = $id_siswa;
						$i++;
					}
					$jumlah_siswa = $i;
					for ($i = 0; $i < $jumlah_siswa; $i++)
					{
						for ($j = 0; $j < $jumlah_siswa - 1; $j++)
						{
							if ($rank[$j]['nilai'] < $rank[$j + 1]['nilai'])
							{
								$data_nilai = $rank[$j]['nilai'];
								$data_id_siswa = $rank[$j]['id_siswa'];
								$rank[$j]['nilai'] = $rank[$j + 1]['nilai'];
								$rank[$j]['id_siswa'] = $rank[$j + 1]['id_siswa'];
								$rank[$j + 1]['nilai'] = $data_nilai;
								$rank[$j + 1]['id_siswa'] = $data_id_siswa;
							}
						}
					}
				?>
				<!-- <input type="text" name="agama" id="agama" value="<?=$matpel?>" />
				<input type="text" name="agama" id="agama" value="<?=$semester?>" /> -->
				<!-- <input type="text" name="agama" id="agama" value="<?=$semester?>" /> -->
			<tbody>
				<?php
						$i = 1;
						// $counter_ulangan = 0;
						// $counter_tugas = 0;
						// $counter_ujian = 0;
						$query_siswa = mysql_query("SELECT * FROM siswa WHERE id_kelas='$id_kelas'");
						$max_nilai = -1;
						$min_nilai = -1;
						$jumlah_nilai = 0;
						while($siswa_data = mysql_fetch_assoc($query_siswa))
						{
							// $id_siswa = $nilai_data['id_siswa'];
							// $id_matpel = $nilai_data['id_matpel'];
							// $nilai_akhir = $nilai_data['nilai'];
							// $matpel_query = mysql_query("SELECT * FROM mata_pelajaran WHERE id='$id_matpel'");
							// $data_matpel = mysql_fetch_assoc($matpel_query);
							// $kkm = $data_matpel['kkm'];
							// if ($nilai_akhir >= $kkm) {
							// 	$hasil = "KKM tercapai";
							// }else{
							// 	$hasil = "KKM tidak tercapai"; 
							// }

							$semester = $_POST['semester'];
							$id_siswa = $siswa_data['NIS'];
							$nilai_query = mysql_query("SELECT AVG(nilai) AS average FROM nilai WHERE tipe_nilai='Nilai Akhir' AND id_kelas='$id_kelas' AND id_siswa='$id_siswa' AND semester='$semester' ");
							$data_nilai_akhir = mysql_fetch_assoc($nilai_query);

							$jumlah_nilai += $data_nilai_akhir['average'];
							if ($max_nilai == -1 || $max_nilai < $data_nilai_akhir['average'])
							{
								$max_nilai = $data_nilai_akhir['average'];
							}
							if ($min_nilai == -1 || $min_nilai > $data_nilai_akhir['average'])
							{
								$min_nilai = $data_nilai_akhir['average'];
							}

							$peringkat = 0;
							for ($j = 0; $j < $jumlah_siswa; $j++)
							{
								if ($rank[$j]['id_siswa'] == $id_siswa)
								{
									$peringkat = $j + 1;
									break;
								}
							}
						 echo 
						 '
						 	<tr>
						 		<td>'.$i.'</td>
								<td>'.$siswa_data['NIS'].'</td>
								<td>'.$siswa_data['nama'].'</td>
								<td>'.sprintf('%0.2f', $data_nilai_akhir['average']).'</td>
								<td>'.$peringkat.'</td>
							</tr>
						 
						 ';
						 $i++;
						}	
					?>
			</tbody>
		</table>
		<br>
		<br>

		<table>
			<tbody>
				<tr>
					<td> Nilai Tertinggi 
					</td>
					<td>:
					</td>
					<td>
						<?=sprintf('%0.2f', $max_nilai)?>
					</td>
				</tr>
				<tr>
					<td> Nilai Terendah
					</td>
					<td>:
					</td>
					<td>
						<?=sprintf('%0.2f', $min_nilai)?>
					</td>
				</tr>
				<tr>
					<td> Rata-rata nilai
					</td>
					<td>:
					</td>
					<td>
						<?=sprintf('%0.2f', $jumlah_nilai / $jumlah_siswa)?>
					</td>
				</tr>
			</tbody>
		</table>
    </div><!-- end of right content-->
            
                    
  </div>   <!--end of center content -->               
                    
                    
    
    
    <div class="clear"></div>
    </div> <!--end of main content-->
	
    
    <div class="footer">
        	<?php include 'footer.php'; ?>

    	
    </div>

</div>	
</form>	
</body>
</html>